<?php
class Admin_Model_Person extends Zend_Db_Table{
    protected $_name = 'person';
    protected $_primary = 'IDPERSON';
    public function listPerson()
    {
        $data=$this->fetchAll();
        return $data;
        //$sql="select * from person";
        //$db = Zend_Registry::get('db');
        //return $db->fetchAll($sql)->toArray();
    }
    public function getPerson($id)
    {
        return $this->fetchRow("IDPERSON=".$id);
    }
    public function getPersonUser($id)
    {
        return $this->fetchRow("IDUSER=".$id);
    }
	public function  addPerson($idProvince,$LastName,$FirstName,$Birthday,$Birthplace,$Gender,$Avatar,$Address,$Email,$Phone)
    {
        $data = array( 
            'IDPERSON'    =>null,                   
            'IDPROVINCES'    =>$idProvince,
            'LASTNAME'    =>$LastName,
            'FIRSTNAME'    =>$FirstName,
            'BIRTHDAY'    =>$Birthday,
            'BIRTHPLACE'    =>$Birthplace,
            'GENDER'    =>$Gender,
            'AVATAR'    =>$Avatar,
            'ADDRESS'    =>$Address,
            'EMAIL'    =>$Email,
            'PHONE'  => $Phone
        );    
        $this->insert($data);
    }
    public function editPerson($idPerson,$idProvince,$LastName,$FirstName,$Birthday,$Birthplace,$Gender,$Avatar,$Address,$Email,$Phone)
    {
        $data = array(                    
            'IDPROVINCES'    =>$idProvince,
            'LASTNAME'    =>$LastName,
            'FIRSTNAME'    =>$FirstName,
            'BIRTHDAY'    =>$Birthday,
            'BIRTHPLACE'    =>$Birthplace,
            'GENDER'    =>$Gender,
            'AVATAR'    =>$Avatar,
            'ADDRESS'    =>$Address,
            'EMAIL'    =>$Email,
            'PHONE'  => $Phone
        );
        $where = array(                    
            'IDPERSON'    =>$idPerson
        );   
        $this->update($data,$where);
    }
    public function delPerson($idPerson)
    {        
        $where = array(                    
            'IDPERSON'    =>$idPerson
        );   
        $this->delete($where);
    }
    public function listMembers($b, $e)
    {
        $db = Zend_Registry::get('db');
        $sql="SELECT p.IDUSER,IDPERSON,NICKNAME,FIRSTNAME,LASTNAME,EMAIL,TIMECREATE FROM user u,person p WHERE u.IDUSER=p.IDUSER ORDER BY TIMECREATE DESC LIMIT $b , $e";
        return $db->fetchAll($sql);
    }
    public function listMember($t,$text,$role)
    {
        $db = Zend_Registry::get('db');
        $type=$t;
        $where=" AND ".$type." like '%".$text."%' ";
        if($t=='IDUSER')
        {
            $type='p.IDUSER';
            if(!is_numeric($text))
            {
                $where="";
            }
            else 
            {
                $where=" AND ".$type." = ".$text;
            }
        }
        if($t=='IDPERSON')
        {
            if(!is_numeric($text))
            {
                $where="";
            }
            else 
            {
                $where=" AND ".$type." = ".$text;
            }
        }
        if($role!='all')
        {
            $where=$where." AND IDROLE=".$role;
        }
        $sql="SELECT p.IDUSER,IDPERSON,NICKNAME,FIRSTNAME,LASTNAME,EMAIL,TIMECREATE FROM user u,person p WHERE u.IDUSER=p.IDUSER $where ORDER BY TIMECREATE DESC";
        return $db->fetchAll($sql);
    }
}
?>